<template>
  <div class="menu-search-dialog">
    <i @click="handleOpen" :class="'iconfont icon-sousuo'" class="toolBar-icon"></i>
    <el-dialog v-model="isShowSearch" destroy-on-close :modal="false" :show-close="false" fullscreen @click="closeSearch">
      <div class="search-container">
        <el-input
          v-model="searchMenu"
          ref="menuInputRef"
          placeholder="菜单搜索 ：支持菜单名称、路径"
          @input="handleSearch"
          @click.stop
        >
          <template #prefix>
            <el-icon>
              <Search />
            </el-icon>
          </template>
        </el-input>

        <el-tree
          ref="treeRef"
          :data="treeData"
          :props="treeProps"
          :filter-node-method="filterNodeMethod"
          node-key="path"
          highlight-current
          @node-click="handleClickMenu"
          class="menu-tree"
        >
          <template #default="{ data }">
            <div class="tree-node">
              <!-- <el-icon v-if="data.meta?.icon && isValidIconName(data.meta.icon)">
                <component :is="data.meta.icon"></component>
              </el-icon> -->
              <span class="node-title">{{ data.meta?.title || data.title }}</span>
            </div>
          </template>
        </el-tree>
      </div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, nextTick, watch } from "vue";
import { Search } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
import { useAuthStore } from "@/stores/modules/auth";
import { transTree } from "@/utils/util";
const router = useRouter();
const authStore = useAuthStore();
const menuList = computed(() => authStore.flatMenuListGet.filter((item: Menu.MenuOptions) => !item.meta.isHide));

// 转换为树形结构数据，过滤掉四个阶段和包含Type的数据
const treeData = computed(() => {
  const fourStages = ["Collect", "Arrangement", "Storage", "Utilization"];
  const data = menuList.value
    .filter((item: Menu.MenuOptions) => {
      // 过滤掉四个阶段
      if (fourStages.includes(item.path)) return false;
      // 过滤掉包含Type字段的数据
      if ("Type" in item) return false;
      return true;
    })
    .map((item: Menu.MenuOptions) => ({
      ...item,
      title: item.meta.title,
      children: item.children || []
    }));
  return transTree(data, "MNU_ID", "MNU_Parent");
});

// 树组件配置
const treeProps = {
  children: "children",
  label: "title"
};

// 打开搜索框
const isShowSearch = ref(false);
const menuInputRef = ref();
const treeRef = ref();
const searchMenu = ref("");

const handleOpen = () => {
  isShowSearch.value = true;
  nextTick(() => {
    setTimeout(() => {
      menuInputRef.value.focus();
    });
  });
};

// 搜索窗关闭
const closeSearch = () => {
  isShowSearch.value = false;
  searchMenu.value = "";
};

// 搜索处理
const handleSearch = (value: string) => {
  if (treeRef.value) {
    treeRef.value.filter(value);
  }
};

// 筛选菜单
const filterNodeMethod = (value: string, data: any) => {
  if (!value) return true;
  return (
    data.path.toLowerCase().indexOf(value.toLowerCase()) > -1 ||
    (data.meta?.title || data.title).toLowerCase().indexOf(value.toLowerCase()) > -1
  );
};

// 验证图标名称是否有效
const isValidIconName = (iconName: string): boolean => {
  if (!iconName || typeof iconName !== "string") return false;
  // 检查是否包含空格或特殊字符
  return /^[a-zA-Z][a-zA-Z0-9_-]*$/.test(iconName);
};

// 点击菜单跳转
const handleClickMenu = (menuItem: Menu.MenuOptions | Record<string, any>) => {
  searchMenu.value = "";
  if (menuItem.meta?.isLink) {
    window.open(menuItem.meta.isLink, "_blank");
  } else {
    router.push(menuItem.path);
  }
  closeSearch();
};
</script>

<style scoped lang="scss">
.menu-search-dialog {
  :deep(.el-dialog) {
    background-color: rgb(0 0 0 / 50%);
    border-radius: 0 !important;
    box-shadow: unset !important;
    .el-dialog__header {
      border-bottom: none !important;
    }
  }
  .search-container {
    position: absolute;
    top: 100px;
    left: 50%;
    width: 550px;
    transform: translateX(-50%);
    .el-input {
      margin-bottom: 20px;
      .el-input__wrapper {
        background-color: var(--el-bg-color);
      }
    }
    .menu-tree {
      max-height: 400px;
      overflow-y: auto;
      background-color: var(--el-bg-color);
      border-radius: 4px;
      .tree-node {
        display: flex;
        align-items: center;
        padding: 8px 0;
        .el-icon {
          margin-right: 8px;
          font-size: 16px;
        }
        .node-title {
          font-size: 14px;
        }
      }
    }
  }
}
</style>
